var salt = sessionStorage.getItem('usersalt');

layui.use(['form', 'layer', 'table','laypage', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laytpl = layui.laytpl,
		laypage = layui.laypage;

	/*验证登录*/
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);
	}
	
	//渲染ul的高度
	function setUlHeight(){
		var bodyHeight = $("body").height();
		var firFormHeight = $(".firForm").height();
		var secFormHeight = $(".secForm").height();
		var pageHeight = $(".pagediv").height();
		$("ul").height(bodyHeight - firFormHeight - secFormHeight - 14 - pageHeight - 60);
	}
	setUlHeight();

	$.post(url + "/baseUsers/getInfo",{Token:salt},function (data) {
		var data = data.data;
        $(".kcgl").hide();
		if(data.roleId == 1){
			$(".kcgl").show()
		}
    })
	
	/*取出所有的父级岗位*/
	var post;
	function getParentPost() {
		$.ajax({
			url: url + 'post/getParentPost',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
			},
			success: function(data) {
				if(data.code == 0) {
					post = data.data;

					//渲染岗位html
					var postType = $("#postType").html();
					laytpl(postType).render(post, function(html){
					  	$("#postTypeView").html(html);

					  	//html点击事件
					  	$("body").click(function() {
							$("#postSelect").hide();
						})
						$('#postInput').click(function() {
							$("#postSelect").show();
							return false;
						});
					});
				}
			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}
	getParentPost();
	
	//选择岗位业务
	window.choosePostType = function(id, name){
		$("#postTypeId").val(id);
		$("#postTypeName").val(name);
		$(".postType").removeClass("layui-this");
		$("#postType" + id).addClass("layui-this");
	};
	
	/*取出课程分类*/
	var courseTypes;
	function getCourseType() {
		$.ajax({
			url: url + 'course/getCourseType',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
			},
			success: function(data) {
				if(data.code == 0) {
					courseTypes = data.data;

					//渲染课程分类
					var courseType = $("#courseType").html();
					laytpl(courseType).render(courseTypes, function(html){
					  	$("#courseTypeView").html(html);
					  	
					  	//html点击事件
					  	$("body").click(function() {
							$("#courseSelect").hide();
						})
						$('#courseInput').click(function() {
							$("#courseSelect").show();
							return false;
						});
					});
				}
			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}
	getCourseType();
	
	//选择课程分类
	window.chooseCourseType = function(id, name){
		$("#courseTypeId").val(id);
		$("#courseTypeName").val(name);
		$(".courseType").removeClass("layui-this");
		$("#courseType" + id).addClass("layui-this");
	};
	
	//筛选
	$("#selList,#search").on("click", function(){
		getTrainPlatformList();
	});

	//获取培训课程列表
	var pageObj = {page : 1,limit : 10,total : -1};
	var record = {url : "",list : []};
	function getTrainPlatformList(){
		var inptext = $('#inptext').val();
		var box = /\s/g;
		var searchText = inptext.replace(box, '');
		var postType = $("#postTypeId").val();
		var courseType = $("#courseTypeId").val();
		$.ajax({
			url: url + "course/getPage",
			dataType: "json",
			type: "post",
			async: true,
			data: {
				Token: salt,
				page: pageObj.page,
				limit: pageObj.limit,
				name: searchText,
				postTypeId: postType,
				courseTypeId: courseType
			},
			success: function(data) {
				if (pageObj.total == -1) {//页面初始化
					pageObj.total = data.count;
					initPaging();
				}
				
				record.url = url;
				record.list = data.data;
				//渲染课程列表html
				var images = $("#images").html();
				laytpl(images).render(record, function(html){
				  	$("#imagesView").html(html);
	
					//图片高度
					$(".ulimg li img").height($(".ulimg li img").width() * 0.66);
				});
			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}
	getTrainPlatformList();
	
	//页码
	function initPaging(){
	  	laypage.render({
	    	elem: 'paging',
	    	count: pageObj.total,
	    	prev: "<i class=\"layui-icon\"></i>",
	    	next: "<i class=\"layui-icon\"></i>",
	    	layout: ['prev', 'page', 'next', 'refresh', 'skip', 'count', 'limit'],
	    	jump: function(obj){
	    		pageObj.page = obj.curr;
	    		pageObj.limit = obj.limit;
	      		getTrainPlatformList();
	    	}
	  	});
	}
	
	//下载
	$("body").on("click",".downLoad",function () {
		var fileId = $(this).attr("fileId");
        window.open(url + '/attachment/seekFile/' + fileId);
    })

	//课程详情
	window.showDetail = function(index){
		var course = record.list[index];
		if (course.fileExt == "ppt" || course.fileExt == "pptx") {
			location.href = "../ppt-detail/ppt.html?id=" + course.id;
		} else {
			location.href = "../video-detail/video.html?id=" + course.id;
		}
	};
})
